package com.bianhua.controller;

import com.bianhua.entity.Company;
import com.bianhua.framework.redis.RedisUtil;
import com.bianhua.service.CompanyService;
import com.bianhua.utils.Result;
import com.bianhua.utils.UserThreadLocal;
import com.github.pagehelper.PageInfo;
import com.bianhua.vo.UserData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    @Autowired
    private RedisUtil redisUtil;

    @PostMapping("create")
    public Result create(@RequestBody Company company){
        int flag = companyService.create(company);
        if(flag>0){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    @GetMapping("delete")
    public Result delete(String ids){
        int flag = companyService.delete(ids);
        if(flag>0){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    @PostMapping("update")
    public Result update(@RequestBody Company company){
        int flag = companyService.update(company);
        if(flag>0){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }


    @PostMapping("updateSelective")
    public Result updateSelective(@RequestBody Company company){
        int flag = companyService.updateSelective(company);
        if(flag>0){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    @PostMapping("pwd")
    public Result updatePwd(@RequestBody Map<String,String> map){
        Integer id = Integer.parseInt(map.get("id"));
        //原密码
        String  spassword = map.get("spassword");
        //新密码
        String  npassword = map.get("npassword");
        Company detail = companyService.detail(id);
        if(!spassword.equals(detail.getPassword())){
            return Result.fail("原密码错误");
        }
        Company company = new Company();
        company.setId(id);
        company.setPassword(npassword);
        int flag = companyService.updateSelective(company);
        if(flag>0){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }


    @GetMapping("detail")
    public Result detail(Integer id){
        return  Result.ok(companyService.detail(id));
    }

    @GetMapping("info")
    public Result info(){
        String token = UserThreadLocal.get();
        UserData userData = (UserData) redisUtil.get(token);
        return  Result.ok(companyService.detail(userData.getId()));
    }

    @PostMapping("query")
    public Map<String,Object> query(@RequestBody  Company company){
        PageInfo<Company> pageInfo = companyService.query(company);
        return Result.ok(pageInfo);
    }

}
